package com.lysj.bhp.api.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.util.web.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v2/user")
@Api(value = "ApiSubstituteController", tags = "官方代充接口接口")
public class ApiSubstituteController extends ApiContent {

    @ApiOperation(value = "官方代充", notes = "进入官方代充界面,显示1BHPC对应CNY跟USD数量")
    @GetMapping("/substitute_ratio")
    public Resp substituteRatio() {
        Map<String, Object> map = new HashMap<>();
        BigDecimal bhpcToCnyRate = exchangeRate.getBHPC_TO_CNY();
        BigDecimal bhpcToDollar = exchangeRate.getBHPC_TO_DOLLAR();
        map.put("bhpcToCnyRate", bhpcToCnyRate.setScale(6, BigDecimal.ROUND_HALF_DOWN).doubleValue());
        map.put("bhpcToDollar", bhpcToDollar.setScale(6, BigDecimal.ROUND_HALF_DOWN).doubleValue());
        map.put("contant", TRANSFER_CONTANT);
        map.put("wechat", TRANSFER_WECHAT);
        return new Resp(map);
    }

    @ApiOperation(value = "充值计算", notes = "人民币、美金、BHPC的3方联动,type:输入1为人民币,输入2为美元,输入3为BHPC币")
    @PostMapping("/official_substitute")
    public Resp officialSubstitute(double sum, int type) {
        if (new BigDecimal(sum).compareTo(BigDecimal.valueOf(0)) != 1) {
            return new Resp(Resp.PARAM_ERROR, "请输入正确的金额");
        }
        if (type < 1 || type > 3) {
            return new Resp(Resp.PARAM_ERROR, "系统错误");
        }

        Map<String, Object> map = new HashMap<>();
        BigDecimal dollarToCnyRate = exchangeRate.getDOLLAR_TO_CNY();
        BigDecimal bhpcToCnyRate = exchangeRate.getBHPC_TO_CNY();
        System.out.println("++++++++++++===============" + dollarToCnyRate+" "+bhpcToCnyRate);
        //输入为人民币
        if (type == 1) {
            BigDecimal cnyToDollarRate = new BigDecimal(sum).divide(dollarToCnyRate,6, BigDecimal.ROUND_DOWN);
            BigDecimal cnyToBhpcRate = new BigDecimal(sum).divide(bhpcToCnyRate, 6, BigDecimal.ROUND_DOWN);

            map.put("USD", cnyToDollarRate);
            map.put("BHPC", cnyToBhpcRate);
        }
        //输入为美元
        if (type == 2) {
            BigDecimal dollarToBhpcRate = (new BigDecimal(sum).multiply(dollarToCnyRate)).divide(bhpcToCnyRate, 6, BigDecimal.ROUND_DOWN);

            map.put("CNY", new BigDecimal(sum).multiply(dollarToCnyRate));
            map.put("BHPC", dollarToBhpcRate);
        }
        //输入为BHPC币
        if (type == 3) {
            BigDecimal bhpcToDollarRate = (new BigDecimal(sum).multiply(bhpcToCnyRate)).divide(dollarToCnyRate, 6, BigDecimal.ROUND_DOWN);

            map.put("CNY", new BigDecimal(sum).multiply(bhpcToCnyRate).setScale(6,BigDecimal.ROUND_DOWN));
            map.put("USD", bhpcToDollarRate);
        }
        return new Resp(map);
    }

}
